// Chemical Dosage Control Agent
// 化学加药控制Agent
const Agent = require('./Agent');

class ChemicalDosageAgent extends Agent {
  constructor() {
    super('ChemicalDosageAgent', 'Controls chemical dosage based on water quality');
    // 根据水质控制化学加药
    this.targetRanges = {
      turbidity: { min: 0.5, max: 1.5 }, // NTU 浊度目标范围
      residualChlorine: { min: 0.3, max: 0.8 } // mg/L 余氯目标范围
    };
    this.lastAdjustment = null;
  }

  // Process chemical dosage data
  // 处理化学加药数据
  processData() {
    if (!this.data) return;
    
    console.log(`[${this.name}] Current dosage status:`, {
      pump1: `${this.data.pump1.status} (${this.data.pump1.frequency.toFixed(1)} Hz)`,
      pump2: `${this.data.pump2.status} (${this.data.pump2.frequency.toFixed(1)} Hz)`,
      totalToday: `${this.data.totalDosageToday.toFixed(2)} kg`
    });
    
    console.log(`[${this.name}] 当前加药状态:`, {
      pump1: `${this.data.pump1.status} (${this.data.pump1.frequency.toFixed(1)} Hz)`,
      pump2: `${this.data.pump2.status} (${this.data.pump2.frequency.toFixed(1)} Hz)`,
      totalToday: `${this.data.totalDosageToday.toFixed(2)} kg (今日总量)`
    });
  }

  // Adjust chemical dosage based on water quality
  // 根据水质调整化学加药
  adjustDosage(waterQualityData) {
    if (!waterQualityData || !this.data) {
      return;
    }

    console.log(`[${this.name}] Evaluating dosage adjustment needs`);
    console.log(`[${this.name}] 评估加药调整需求`);

    // In a real implementation, this would use more sophisticated algorithms
    // 在实际实现中，这将使用更复杂的算法
    // For simulation, we'll use simple proportional control
    // 在模拟中，我们使用简单的比例控制
    
    // Check if we need to adjust based on turbidity
    // 检查是否需要根据浊度进行调整
    if (waterQualityData.turbidity > this.targetRanges.turbidity.max) {
      console.log(`[${this.name}] Turbidity high (${waterQualityData.turbidity.toFixed(2)} NTU), increasing coagulant dosage`);
      console.log(`[${this.name}] 浊度过高 (${waterQualityData.turbidity.toFixed(2)} NTU)，增加混凝剂加药量`);
      // Decision logic would go here to send command to increase dosage
      // 决策逻辑将发送增加加药量的命令
    } else if (waterQualityData.turbidity < this.targetRanges.turbidity.min) {
      console.log(`[${this.name}] Turbidity low (${waterQualityData.turbidity.toFixed(2)} NTU), decreasing coagulant dosage`);
      console.log(`[${this.name}] 浊度过低 (${waterQualityData.turbidity.toFixed(2)} NTU)，减少混凝剂加药量`);
      // Decision logic would go here to send command to decrease dosage
      // 决策逻辑将发送减少加药量的命令
    }

    // Check if we need to adjust based on residual chlorine
    // 检查是否需要根据余氯进行调整
    if (waterQualityData.residualChlorine < this.targetRanges.residualChlorine.min) {
      console.log(`[${this.name}] Residual chlorine low (${waterQualityData.residualChlorine.toFixed(2)} mg/L), increasing chlorine dosage`);
      console.log(`[${this.name}] 余氯过低 (${waterQualityData.residualChlorine.toFixed(2)} mg/L)，增加氯气加药量`);
      // Decision logic would go here to send command to increase dosage
      // 决策逻辑将发送增加加药量的命令
    } else if (waterQualityData.residualChlorine > this.targetRanges.residualChlorine.max) {
      console.log(`[${this.name}] Residual chlorine high (${waterQualityData.residualChlorine.toFixed(2)} mg/L), decreasing chlorine dosage`);
      console.log(`[${this.name}] 余氯过高 (${waterQualityData.residualChlorine.toFixed(2)} mg/L)，减少氯气加药量`);
      // Decision logic would go here to send command to decrease dosage
      // 决策逻辑将发送减少加药量的命令
    }

    // Record the adjustment time
    // 记录调整时间
    this.lastAdjustment = new Date();
  }

  // Get current dosage efficiency
  // 获取当前加药效率
  getDosageEfficiency(waterQualityData) {
    if (!waterQualityData || !this.data) {
      return null;
    }

    // Simple efficiency calculation based on target ranges
    // 基于目标范围的简单效率计算
    const turbidityEfficiency = this.calculateParameterEfficiency(
      waterQualityData.turbidity,
      this.targetRanges.turbidity.min,
      this.targetRanges.turbidity.max
    );

    const chlorineEfficiency = this.calculateParameterEfficiency(
      waterQualityData.residualChlorine,
      this.targetRanges.residualChlorine.min,
      this.targetRanges.residualChlorine.max
    );

    return {
      turbidityEfficiency,
      chlorineEfficiency,
      overallEfficiency: (turbidityEfficiency + chlorineEfficiency) / 2,
      timestamp: new Date()
    };
  }

  // Calculate efficiency for a parameter
  // 计算参数效率
  calculateParameterEfficiency(value, min, max) {
    if (value >= min && value <= max) {
      // In target range - 100% efficient
      // 在目标范围内 - 100%效率
      return 100;
    } else if (value < min) {
      // Below minimum - efficiency decreases linearly
      // 低于最小值 - 效率线性下降
      return Math.max(0, 100 - ((min - value) / min) * 100);
    } else {
      // Above maximum - efficiency decreases linearly
      // 高于最大值 - 效率线性下降
      return Math.max(0, 100 - ((value - max) / max) * 100);
    }
  }
}

module.exports = ChemicalDosageAgent;